//
//  SAShopPreviewCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/10.
//

#import "SAShopPreviewCell.h"
#import "SAProductOverModel.h"

@interface SAShopPreviewCell ()

@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *shopIdLab;
@property (nonatomic, strong) UILabel *shopTypeLab;
@property (nonatomic, strong) UILabel *shopNameLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *dateLab;

@end

@implementation SAShopPreviewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAProductOverModel *)model {
    _model = model;

    NSString *newUrl = [[model.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopIdLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    self.shopTypeLab.text = [NSString stringWithFormat:@"库存:%@",model.stock];
    self.shopNameLab.text = [NSString stringWithFormat:@"零售价:%0.2f",[model.retailPrice doubleValue]/100.f];
    
    self.priceLab.text = model.categoryName;
    self.dateLab.text = model.onsalesTime;
}

-(void)setIsRedBg:(BOOL)isRedBg {
    _isRedBg = isRedBg;
    if (isRedBg == YES) {
        self.backgroundColor = kColor(255, 228, 231, 1);
    }else {
        self.backgroundColor = [UIColor whiteColor];
    }
}

-(void)clickedImageV {
    !self.showImageBlock ?: self.showImageBlock();
}

#pragma mark - initUI
-(void)initUI {

    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.shopIdLab];
    [self.contentView addSubview:self.shopTypeLab];
    [self.contentView addSubview:self.shopNameLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.dateLab];
    [self.contentView addSubview:self.line];
}

#pragma mark - lazy
-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:SARectMake(26, 6, 53, 59)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageV)];
        _imageV.userInteractionEnabled = YES;
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
        [_imageV addGestureRecognizer:tap];
    }
    return _imageV;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(0, 70, 375, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UILabel *)shopIdLab {
    if (!_shopIdLab) {
        _shopIdLab = [[UILabel alloc] initWithFrame:SARectMake(85, 5, 150, 20)];
        _shopIdLab.textColor = [UIColor blackColor];
        _shopIdLab.font = kFont(13);
    }
    return _shopIdLab;
}

-(UILabel *)shopTypeLab {
    if (!_shopTypeLab) {
        _shopTypeLab = [[UILabel alloc] initWithFrame:SARectMake(85, 25, 150, 20)];
        _shopTypeLab.textColor = kLightgGrayColor;
        _shopTypeLab.font = kFont(13);
    }
    return _shopTypeLab;
}

-(UILabel *)shopNameLab {
    if (!_shopNameLab) {
        _shopNameLab = [[UILabel alloc] initWithFrame:SARectMake(85, 45, 150, 20)];
        _shopNameLab.textColor = kLightgGrayColor;
        _shopNameLab.font = kFont(13);
    }
    return _shopNameLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(250, 25, 100, 20)];
        _priceLab.textColor = kLightgGrayColor;
        _priceLab.textAlignment = NSTextAlignmentRight;
        _priceLab.font = kFont(13);
    }
    return _priceLab;
}

-(UILabel *)dateLab {
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] initWithFrame:SARectMake(250, 45, 100, 20)];
        _dateLab.textColor = kLightgGrayColor;
        _dateLab.textAlignment = NSTextAlignmentRight;
        _dateLab.font = kFont(13);
    }
    return _dateLab;
}

@end
